﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S.Archives.Domain.S.ArchivesDto;
using S.Archives.Read.Api.Application.Command;
using S.Archives.Read.Api.Application.CommandHandlers;
using S.Archives.Read.Api.Dto;
using S.Common.Enums.Archives;
using System;

namespace S.Archives.Read.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SheepFileModelController : ControllerBase
    {
        private IMediator mediator;
        public SheepFileModelController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 枚举获取基因等级
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetGeneRankEnum")]
        public IActionResult GetGeneRankEnum()
        {
            List<EnumDto> enumDtos = new List<EnumDto>();
            foreach (var item in Enum.GetValues<GeneRankEnum>())
            {
                enumDtos.Add(new EnumDto
                {
                    Value = ((int)item),
                    Label = item.ToString(),
                });
            }
            return Ok(enumDtos);
        }
        /// <summary>
        /// 枚举获取品种
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetVarietyEnum")]
        public IActionResult GetVarietyEnum()
        {
            List<EnumDto> enumDtos = new List<EnumDto>();
            foreach (var item in Enum.GetValues<VarietyEnum>())
            {
                enumDtos.Add(new EnumDto
                {
                    Value = ((int)item),
                    Label = item.ToString(),
                });
            }
            return Ok(enumDtos);
        }
        /// <summary>
        /// 获取羊只类型枚举
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet("GetSheepTypeEnum")]
        public IActionResult GetSheepTypeEnum()
        {
            List<EnumDto> enumDtos = new List<EnumDto>();
            foreach (var item in Enum.GetValues<SheepType>())
            {
                enumDtos.Add(new EnumDto
                {
                    Value = ((int)item),
                    Label = item.ToString(),
                });
            }
            return Ok(enumDtos);
        }


        /// <summary>
        /// 获取在场状态枚举
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet("GetPresenceStateEnum")]
        public IActionResult GetPresenceStateEnum()
        {
            List<EnumDto> enumDtos = new List<EnumDto>();
            foreach (var item in Enum.GetValues<PresenceStateEnum>())
            {
                enumDtos.Add(new EnumDto
                {
                    Value = ((int)item),
                    Label = item.ToString(),
                });
            }
            return Ok(enumDtos);
        }
        /// <summary>
        /// 获取生产等级枚举
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet("GetProductionRankEnum")]
        public IActionResult GetProductionRankEnum()
        {
            List<EnumDto> enumDtos = new List<EnumDto>();
            foreach (var item in Enum.GetValues<ProductionRankEnum>())
            {
                enumDtos.Add(new EnumDto
                {
                    Value = ((int)item),
                    Label = item.ToString(),
                });
            }
            return Ok(enumDtos);
        }
        /// <summary>
        /// 获取销售等级枚举
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet("GetSalesRankEnum")]
        public IActionResult GetSalesRankEnum()
        {
            List<EnumDto> enumDtos = new List<EnumDto>();
            foreach (var item in Enum.GetValues<SalesRankEnum>())
            {
                enumDtos.Add(new EnumDto
                {
                    Value = ((int)item),
                    Label = item.ToString(),
                });
            }
            return Ok(enumDtos);
        }
        /// <summary>
        /// 获取繁殖状态枚举
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet("GetReproductiveStateEnum")]
        public IActionResult GetReproductiveStateEnum()
        {
            List<EnumDto> enumDtos = new List<EnumDto>();
            foreach (var item in Enum.GetValues<ReproductiveStateEnum>())
            {
                enumDtos.Add(new EnumDto
                {
                    Value = ((int)item),
                    Label = item.ToString(),
                });
            }
            return Ok(enumDtos);
        }
        
        /// <summary>
        /// 获取生长阶段枚举
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet("GetGrowthStageEnum")]
        public IActionResult GetGrowthStageEnum()
        {
            List<EnumDto> enumDtos = new List<EnumDto>();
            foreach (var item in Enum.GetValues<GrowthStageEnum>())
            {
                enumDtos.Add(new EnumDto
                {
                    Value = ((int)item),
                    Label = item.ToString(),
                });
            }
            return Ok(enumDtos);
        }

        /// <summary>
        /// 获取羊只档案
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// 
        [HttpPost("GetSheepFiles")]
        public async Task<IActionResult>  GetSheepFiles(GetSheepFilesCommand t)
        {
            var reqult = await mediator.Send(t);
            return Ok(reqult);
        }
        /// <summary>
        /// 批量配种筛选
        /// </summary>
        /// <param name="pL"></param>
        /// <returns></returns>
        /// 
        [HttpPost("PLBatchBreeding")]
        public async Task<IActionResult>  PLBatchBreeding(PLBatchBreedingCommand pL)
        {
            var result=await mediator.Send(pL);
            return Ok(result);  
        }
        /// <summary>
        /// 判断是否可以配种
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// 
        [HttpPost("IsOkMatingSpecies")]
        public async Task<IActionResult> IsOkMatingSpecies(IsOkMatingSpeciesCommand t)
        {
            if(string.IsNullOrEmpty(t.paternalEITag)|| string.IsNullOrEmpty(t.maternalEITag))
            {
                return Ok(-1);
            }
            else
            {
                var result = await mediator.Send(t);
                return Ok(result);
            }
            
        }
        /// <summary>
        /// 获取羊只系谱
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// 
        [HttpPost("GetSheepGenealogy")]
        public async Task<IActionResult> GetSheepGenealogy(GetSheepGenealogyCommand t)
        {
            var result = await mediator.Send(t);
            return Ok(result);
        }
        /// <summary>
        /// 获取羊只档案详情
        /// </summary>
        /// <param name="t">可视耳标</param>
        /// <returns></returns>
        /// 
        [HttpPost("GetSheepFilesDetails")]
        public async Task<IActionResult> GetSheepFilesDetails(GetSheepFilesDetailsCommand t)
        {
            var result = await mediator.Send(t);
            return Ok(result);
        }
        /// <summary>
        /// 递归显示羊只系谱
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// 
        [HttpPost("GetSheepGenealogyShowList")]


        public async Task<IActionResult> GetSheepGenealogyShowList(GetSheepGenealogyShowListCommand t)
        {
            var result = await mediator.Send(t);
            if (result == null)
            {
                return NotFound("Result not found.");
            }
            FillChildren(result, 3);  // 4层
            return Ok(result);
        }

        private void FillChildren(SheepGenealogyShow node, int remainingLevels)
        {
            if (node == null || remainingLevels <= 0)
            {
                return;
            }

            if (node.children == null)
            {
                node.children = new List<SheepGenealogyShow>();
            }

            if (node.children.Count == 0)
            {
                // 添加两个子节点
                node.children.Add(new SheepGenealogyShow
                {
                    name = "无",
                    children = new List<SheepGenealogyShow>(),
                    Sex = false
                });
                node.children.Add(new SheepGenealogyShow
                {
                    name = "无",
                    children = new List<SheepGenealogyShow>(),
                    Sex = false
                });
            }

            foreach (var child in node.children)
            {
                FillChildren(child, remainingLevels - 1);
            }
        }

        /// <summary>
        /// 根据羊只获取下拉框(未入场)
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// 
        [HttpPost("SelMaleEmaleSelQC")]
        public async Task<IActionResult> SelMaleEmaleSelQC(int id)
        {
            var result = await mediator.Send(new SelMaleEmaleSelQCCommand() { FileType=id} );
            return Ok(result);
        }
        /// <summary>
        /// 后裔转种下拉框
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// 
        [HttpPost("SelMaleEmaleSelSZC")]
        public async Task<IActionResult> SelMaleEmaleSelSZC()
        {
            var result = await mediator.Send(new SelMaleEmaleSelSZCommand());
            return Ok(result);
        }
       /// <summary>
       /// 获取羊只档案(在场)用于体侧
       /// </summary>
       /// <param name="id">0-全部 1-种公 2-种母 3-后裔</param>
       /// <returns></returns>
        [HttpGet("GetSheepFilesAll")]
        public async Task<IActionResult> GetSheepFilesAll(int id)
        {
            var result = await mediator.Send(new GetSheepFilesAllCommand() { Id=id});
            return Ok(result);
        }
        /// <summary>
        /// 根据耳标精确查询羊只档案信息用于反填
        /// </summary>
        /// <param name="EarTag"></param>
        /// <returns></returns>
        /// 
        [HttpGet("GetSheepFilesByEarTag")]
        public async Task<IActionResult> GetSheepFilesByEarTag(string EarTag)
        {
            var result = await mediator.Send(new GetSheepFilesByEarTagCommand() { EarTag= EarTag });
            return Ok(result);
        }
        /// <summary>
        /// 可以销售的耳号下拉框
        /// </summary>
        /// <param name="id">0-全部1-种公2-种母3-后裔</param>
        /// <returns></returns>
        
        [HttpGet("SalesEarTagSel")]
        public async Task<IActionResult> SalesEarTagSel(int id)
        {
            var result = await mediator.Send(new SalesEarTagSelCommand() {id=id});
            return Ok(result);
        }


    }
}
